<template>
  <div>
    <!-- <form action="" method="post" enctype="multipart/form-data">
      <input type="file">
    </form> -->
    <Loadering v-if="isLoading" text="我已经拼命加载了"></Loadering>
    <!--头部导航-->
    <global-header :user="currentUser"></global-header>
    <!-- <uploader-file
      action="/upload"
      :beforeUpload="beforeUpload"
      @file-uploaded="onFileUploaded"
    >
      <h2>点击上传</h2>
      <template #loading>
        <div class="spinner-boder" role="status">
          <span class="sr-only"></span>
        </div>
      </template>

      <template #uploaded="dataProps">
        <img :src="dataProps.uploadedData.data.url" alt="" width="500">
      </template>
    </uploader-file> -->
    <router-view></router-view>
    <footer class="text-center py-4 text-secondary bg-light mt-6">
      <small>
        <ul class="list-inline mb-0">
          <li class="list-inline-item">专栏</li>
          <li class="list-inline-item">文档</li>
          <li class="list-inline-item">详情</li>
          <li class="list-inline-item">地址</li>
          <li class="list-inline-item">联系</li>
        </ul>
      </small>
    </footer>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, onMounted, ref, watch } from "vue";
import GlobalHeader from "@/components/GlobalHeader.vue";
import { useStore } from "vuex";
import Loadering from "./components/Loadering.vue";
import createMessage from "./hook/createMessage";
import { GolbalDateProps, ImageProps,ResponseType } from "./store";
// import axios from "axios";
// import UploaderFile from "./components/UploaderFile.vue";

//用户消息和登录状态
// const currentUser: Userprops = {
//   isLogin: false,
//   name: '迪迦奥特曼',
// }

export default defineComponent({
  name: "App",
  components: {
    GlobalHeader,
    Loadering,
    // UploaderFile,
    // MessageIndex
  },
  setup() {
    const store = useStore<GolbalDateProps>();
    const currentUser = computed(() => store.state.user);
    const isLoading = computed(() => store.state.loading);
    const token = computed(() => store.state.token);
    const error = computed(() => store.state.error);
    onMounted(() => {
      // 此处 xxx （为获取的用户名，为xxx 表示没有获取用户名）
      // isLogin 为 fakse 头部显示的为登录和注册 就算是 token 值保存到本地了也没有意义
      // isLogin 为 true 头部显示的 你好 xxx 此时xxx并没有获取到用户信息，前提是已经保存了 token
      // 为什么 isLogin 要取反 它的初始值为 false 在第一次登录之后值为ture 但是在刷新的时候就会变回初始值 false
      // if (!currentUser.value.isLogin && token.value) {
      //   // 添加请求头 默认值 携带 token
      //   axios.defaults.headers.common.Authorization = `Bearer ${token.value}`;
      //   // 再次获取用户信息，使 xxx 变成我们登录的用户名
      //   store.dispatch("fetchCurrentUser");
      // }
      // 监听状态
      watch(
        () => error.value.status,
        () => {
          const { status, message } = error.value;
          if (status && message) {
            createMessage(message, "error");
          }
        }
      );
    });
    // 验证传入的图片格式
    // const beforeUpload = (file: File) => {
    //   console.log(file);
    //   const isJPG = file.type === "image/jpeg";
    //   if (!isJPG) {
    //     createMessage("上传图片只能是 JPG 格式", "error");
    //   }
    //   return isJPG;
    // };
    // const onFileUploaded = (rawData: ResponseType<ImageProps>) => {
    //   createMessage(`上传图片 ID ${rawData.data._id}`, "success");
    // };
    return {
      currentUser,
      isLoading,
      error,
      // beforeUpload,
      // onFileUploaded
    };
  },
});
</script>

<style></style>
